package com.study.es.Item.service.impl;

import com.study.es.Item.dao.EsRepository;
import com.study.es.Item.document.Item;
import com.study.es.Item.service.EsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

@Service
public class EsServiceImpl implements EsService {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    private EsRepository esRepository;

    @Override
    public Item select(Long id) {
        return esRepository.getItemById(id);
    }

    @Override
    public void update() {
        Item item = new Item();
        item.setId(1L);
        item.setImages("默认图片地址");
        item.setTitle("华为手机");
        item.setPrice(99D);
        item.setBrand("华为");
        item.setCategory("手机");
        esRepository.save(item);
    }

    @Override
    public void delete(Long id) {
        esRepository.deleteById(id);
    }

    @Override
    public void add() {
        Item item = new Item();
        item.setId(1L);
        item.setImages("默认图片地址");
        item.setTitle("小米手机");
        item.setPrice(100D);
        item.setBrand("小米");
        item.setCategory("手机");
        esRepository.save(item);
    }

    @Override
    public void deleteIndex() {
        elasticsearchTemplate.deleteIndex(Item.class);
    }

    @Override
    public void createIndex() {
        elasticsearchTemplate.createIndex(Item.class);
        elasticsearchTemplate.putMapping(Item.class);
    }
}
